package com.community.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.communit.dto.MyCommentDto;
import com.community.entity.ArticleCommet;
import com.community.service.IArticleCommetService;
import com.community.service.IArticleService;

@Controller
@RequestMapping("/mycomment")
public class MyCommentController {
	
	@Autowired
	private IArticleCommetService iArticleCommetService;
	
	@Autowired
	private IArticleService iArticleService;
	
	@RequestMapping()
	public String mycomment(HttpSession session,ModelMap modelMap){
		if(session.getAttribute("userid") == null){
			return "/user/login";
		}else{
			Long userid = Long.valueOf(session.getAttribute("userid").toString());
			List<ArticleCommet> acList = iArticleCommetService.getArticleCommetListByuserId(userid);
			List<MyCommentDto> mcList = new ArrayList<>();
			MyCommentDto mdto;
			for (ArticleCommet ArticleCommet : acList) {
				mdto = new MyCommentDto();
				mdto.setArticleid(ArticleCommet.getArticleid());
				mdto.setCommentContent(ArticleCommet.getCommentContent());
				mdto.setTittle(iArticleService.getArticleById(ArticleCommet.getArticleid()).getTittle());
				mdto.setCreateTime(ArticleCommet.getCreateTime());
				mdto.setId(ArticleCommet.getId());
				mcList.add(mdto);
			}
			modelMap.addAttribute("mcList", mcList);
			return "/mycomment/comment";
		}
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public int deleteComment(Long Id){
		ArticleCommet a= iArticleCommetService.getArticleCommetById(Id);
		iArticleCommetService.deleteArticleCommet(a);
		return 0;
	}

}
